#include "stm32f4xx_hal.h"
#include "stdio.h"
#include "DevicesADC.h"
#include "DevicesRT8092.h"
#include "DriverCurrent.h"


static CurrentInfoType st_typeCurrentInfo = {0.0f};


void vCurrentInit(void)
{
    st_typeCurrentInfo.vdd1.limit = 1.0f;
    st_typeCurrentInfo.vdd2H.limit = 1.0f;
    st_typeCurrentInfo.vdd2L.limit = 1.0f;
    st_typeCurrentInfo.vddQ.limit = 1.0f;
}

float fCurrentGet(uint32_t uiChannel)
{
    float fVoltage = 0, fCurrent = 0;

    switch(uiChannel)
    {
        case CURRENT_CHANNEL_VDD1  :
            fVoltage = fADCGetValue(ADC_CHANNEL_CURRENT_VCC1);
            /* 采样电阻为0.1ohm，运算再放大50倍 */
            fCurrent = fVoltage * (10.0f / 50.0f);
            break;

        case CURRENT_CHANNEL_VDD2H  :
            fVoltage = fADCGetValue(ADC_CHANNEL_CURRENT_VCC2H);
            /* 采样电阻为0.1ohm，运算再放大50倍 */
            fCurrent = fVoltage * (10.0f / 50.0f);
            break;

        case CURRENT_CHANNEL_VDD2L  :
            fVoltage = fADCGetValue(ADC_CHANNEL_CURRENT_VCC2L);
            /* 采样电阻为0.1ohm，运算再放大50倍 */
            fCurrent = fVoltage * (10.0f / 50.0f);
            break;

        case CURRENT_CHANNEL_VDDQ  :
            fVoltage = fADCGetValue(ADC_CHANNEL_CURRENT_VCCQ);
            /* 采样电阻为0.1ohm，运算再放大50倍 */
            fCurrent = fVoltage * (10.0f / 50.0f);
            break;

        default : break;
    }

    return fCurrent;
}

void vCurrentUpdate(void)
{
    st_typeCurrentInfo.vdd1.get  = fCurrentGet(CURRENT_CHANNEL_VDD1);
    st_typeCurrentInfo.vdd1.getFilter = (st_typeCurrentInfo.vdd1.get * 0.1f) + (st_typeCurrentInfo.vdd1.getFilter * 0.9f);
    st_typeCurrentInfo.vdd1.getMax = (st_typeCurrentInfo.vdd1.getMax < st_typeCurrentInfo.vdd1.get) ? st_typeCurrentInfo.vdd1.get : st_typeCurrentInfo.vdd1.getMax;

    st_typeCurrentInfo.vdd2H.get = fCurrentGet(CURRENT_CHANNEL_VDD2H);
    st_typeCurrentInfo.vdd2H.getFilter = (st_typeCurrentInfo.vdd2H.get * 0.1f) + (st_typeCurrentInfo.vdd2H.getFilter * 0.9f);
    st_typeCurrentInfo.vdd2H.getMax = (st_typeCurrentInfo.vdd2H.getMax < st_typeCurrentInfo.vdd2H.get) ? st_typeCurrentInfo.vdd2H.get : st_typeCurrentInfo.vdd2H.getMax;

    st_typeCurrentInfo.vdd2L.get = fCurrentGet(CURRENT_CHANNEL_VDD2L);
    st_typeCurrentInfo.vdd2L.getFilter = (st_typeCurrentInfo.vdd2L.get * 0.1f) + (st_typeCurrentInfo.vdd2L.getFilter * 0.9f);
    st_typeCurrentInfo.vdd2L.getMax = (st_typeCurrentInfo.vdd2L.getMax < st_typeCurrentInfo.vdd2L.get) ? st_typeCurrentInfo.vdd2L.get : st_typeCurrentInfo.vdd2L.getMax;

    st_typeCurrentInfo.vddQ.get  = fCurrentGet(CURRENT_CHANNEL_VDDQ);
    st_typeCurrentInfo.vddQ.getFilter = (st_typeCurrentInfo.vddQ.get * 0.1f) + (st_typeCurrentInfo.vddQ.getFilter * 0.9f);
    st_typeCurrentInfo.vddQ.getMax = (st_typeCurrentInfo.vddQ.getMax < st_typeCurrentInfo.vddQ.get) ? st_typeCurrentInfo.vddQ.get : st_typeCurrentInfo.vddQ.getMax;
}

CurrentInfoType *ptypeCurrentInfoGet(void)
{
    return &st_typeCurrentInfo;
}
